﻿using Medical.Api.Write.Dto.RBAC;
using Medical.Domain.RBAC;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Medical.Api.Write.Service.RBAC;

namespace Medical.Api.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RBACController : ControllerBase
    {
        private readonly IRBACService rBACService;

        public RBACController(IRBACService rBACService)
        {
            this.rBACService = rBACService;
        }
        /// <summary>
        /// 创建权限
        /// </summary>
        /// <param name="permission">权限信息</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreatePermission([FromBody] PermissionModel permission)
        {
            return rBACService.CreatePermission(permission);
        }
        /// <summary>
        /// 创建角色并分配权限
        /// </summary>
        /// <param name="createRoleDto">创建角色DTO</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateRole([FromBody] CreateRoleDto createRoleDto)
        {
            return rBACService.CreateRole(createRoleDto.Role, createRoleDto.PermissionIds);
        }
        /// <summary>
        /// 创建用户并分配角色
        /// </summary>
        /// <param name="createUserDto">创建用户DTO</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateUser([FromBody] CreateUserDto createUserDto)
        {
            return rBACService.CreateUser(createUserDto.User, createUserDto.RoleIds);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateUser([FromBody] UserModel user)
        {
            return rBACService.UpdateUser(user);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> DeleteUser([FromQuery] int userId)
        {
            return rBACService.DeleteUser(userId);
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateRole([FromBody] RoleModel role)
        {
            return rBACService.UpdateRole(role);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> DeleteRole([FromQuery] int roleId)
        {
            return rBACService.DeleteRole(roleId);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdatePermission([FromBody] PermissionModel permission)
        {
            return rBACService.UpdatePermission(permission);
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> DeletePermission([FromQuery] int permissionId)
        {
            return rBACService.DeletePermission(permissionId);
        }
        /// <summary>
        /// 检查用户名是否唯一
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<bool> CheckUserNameUnique([FromQuery] string userName, [FromQuery] int? userId)
        {
            return rBACService.CheckUserNameUnique(userName, userId);
        }
        /// <summary>
        /// 检查角色名是否唯一
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<bool> CheckRoleNameUnique([FromQuery] string roleName, [FromQuery] int? roleId)
        {
            return rBACService.CheckRoleNameUnique(roleName, roleId);
        }
        /// <summary>
        /// 检查权限名是否唯一
        /// </summary>
        /// <param name="permissionName"></param>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<bool> CheckPermissionNameUnique([FromQuery] string permissionName, [FromQuery] int? permissionId)
        {
            return rBACService.CheckPermissionNameUnique(permissionName, permissionId);
        }
    }
}
